<?php
/*
 * Author:YangBo Created on 2009-1-22:����07:23:25
 * 
 * TODO:TODO
 * as9
 * MessageService
 * 
 */
 require_once("./BaseService.php");
 require_once("./interfaces/IMessageService.php");
 require_once("./dao/Message.php");
 require_once("./util/ServiceUtil.php");
 class MessageService extends BaseService implements IMessageService
 {
 	protected $message;
 	public function __construct()
 	{
 		$this->initializeDB();
 		$this->message = new Message();
 	}
 	/**
 	 * 创建留言
	 * @param $target [messager,pno,content]
	 * @return:success
	*/
 	public function create($target)
 	{
 		$messager = $target["messager"];
 		$pno = $target["pno"];
 		$content = $target["content"];
 		
 		try
 		{
 			$this->sql = "INSERT INTO message(messager,pno,content) VALUES('$messager','$pno','$content')";
 			$this->result = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","create message success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		} 		
 	}
 	/**
	 * 读取留言
	 * @param $targetID pno
	 * @return:$this->message
	*/
 	public function read($targetID)
 	{
 		if($targetID)
 		{
 			$pno = strtolower(trim($targetID));
	 		try
	 		{
	 			$this->sql = "SELECT message.id,message.messager,message.pno," .
	 					"message.content,message.leave_time FROM message WHERE pno='$pno'";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			$numRows = mysql_num_rows($this->result);
	 			for($i=0;$i<$numRows;$i++)
	 			{
	 				$row = mysql_fetch_object($this->result);
	 				$this->message = new Message();
	 				$this->message->id = $row->id;
	 				$this->message->messager = $row->messager;
	 				$this->message->pno = $row->pno;
	 				$this->message->content = $row->content;
	 				$this->message->leave_time = $row->leave_time;
	 				
	 				$this->ret[] = $this->message;
	 			}
	 			return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
 		}else
 		{
 			try
	 		{
	 			$this->sql = "SELECT message.id,message.messager,message.pno," .
	 					"message.content,message.leave_time FROM message";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			$numRows = mysql_num_rows($this->result);
	 			for($i=0;$i<$numRows;$i++)
	 			{
	 				$row = mysql_fetch_object($this->result);
	 				$this->message = new Message();
	 				$this->message->id = $row->id;
	 				$this->message->messager = $row->messager;
	 				$this->message->pno = $row->pno;
	 				$this->message->content = $row->content;
	 				$this->message->leave_time = $row->leave_time;
	 				
	 				$this->ret[] = $this->message;
	 			}
	 			return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
 		}
 		
 	}
 	/**
	 * 更新留言
	 * @param $target [messager,pno,content]
	 * @return:success
	*/
 	public function update($target)
 	{
 		$messager = strtolower(trim($target[0]));
 		$pno = strtolower(trim($target[1]));
 		$content = strtolower(trim($target[2]));
 		
 		try
 		{
 			$this->sql = "UPDATE message SET messager='$messager',content='$content' WHERE pno='$pno'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","update message success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 删除留言
	 * @param $targetID pno
	 * @return:success
	*/
 	public function delete($targetID)
 	{
 		$mno = strtolower(trim($targetID));
 		try
 		{
 			$this->sql = "DELETE FROM message WHERE id='$mno'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","delete message success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	
 	public function __destruct()
 	{
 		
 	}
 }
?>
